/*
*
* ████████╗░█████╗░███████╗██████╗░░█████╗░
* ╚══██╔══╝██╔══██╗██╔════╝██╔══██╗██╔══██╗
* ░░░██║░░░██║░░██║█████╗░░██████╦╝███████║
* ░░░██║░░░██║░░██║██╔══╝░░██╔══██╗██╔══██║
* ░░░██║░░░╚█████╔╝██║░░░░░██████╦╝██║░░██║
* ░░░╚═╝░░░░╚════╝░╚═╝░░░░░╚═════╝░╚═╝░░╚═╝
* 
*  :: 九方通逊-Henry ::   Copyright©2020 tofba.com  
* 
*/
package com.lmexpress.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lmexpress.enums.ApiCredential;
import com.lmexpress.enums.ApiOrderError;
import com.lmexpress.enums.LabelSuffix;
import com.lmexpress.orm.dhlec.ConsigneeAddress;
import com.lmexpress.orm.dhlec.CustomsDetail;
import com.lmexpress.orm.dhlec.PackageDetail;
import com.lmexpress.orm.dhlec.ReturnAddress;
import com.lmexpress.orm.dhlec.Shipment;
import com.lmexpress.orm.dhlec.ShippingCost;
import com.lmexpress.orm.dhlec.Weight;
import com.lmexpress.orm.openapi.ExpressOrderApi;
import com.lmexpress.orm.openapi.OrderInvoice;
import com.lmexpress.orm.rest.ApiLabelResponse;
import com.lmexpress.orm.rest.ApiOrderResponse;
import com.lmexpress.service.ApiService;
import com.lmexpress.util.ApiUtil;
import com.lmexpress.util.HttpClientPoolUtil;
import com.lmexpress.util.HttpClientResult;
import com.lmexpress.util.PropertiesUtil;
import com.lmexpress.util.RegexUtil;

/**
 * 
 * <DHL EC V4版本>
 * 
 * @author Henry(fba02)
 * @version [版本号, 2020-12-09]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class DhlEcClient implements ApiService {
	/**
	 * EWR邮编前三位
	 */
	private final static List<String> EWR = Arrays.asList("005", "010", "011", "012", "013", "014", "015", "016", "017", "018", "019", "020", "021", "022", "023", "024", "025", "026", "027", "028", "029", "030", "031", "032", "033", "034", "035", "036", "037", "038", "039", "040", "041", "042", "043", "044", "045", "046", "047", "048", "049", "050", "051", "052", "053", "054", "055", "056", "057", "058", "059", "060", "061", "062", "063", "064", "065", "066", "067", "068", "069", "070", "071", "072", "073", "074", "075", "076", "077", "078", "079", "080", "081", "082", "083", "084", "085", "086", "087", "088", "089", "099", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155",
			"156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199", "200", "201", "202", "203", "204", "205", "206", "207", "208", "209", "210", "211", "212", "214", "215", "216", "217", "218", "219", "220", "221", "222", "223", "224", "225", "226", "227", "228", "229", "230", "231", "232", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "243", "244", "245", "246", "247", "248", "249", "250", "251", "252", "253", "254", "255", "256", "257", "258", "259", "260", "261", "262", "263", "264", "265", "266", "267", "268", "270", "271", "272", "273", "274", "275", "276", "277", "278", "279", "280", "281", "282", "283", "284", "285", "286", "287", "288", "289", "290", "291", "292", "293", "294", "295", "296", "297", "298", "299", "300", "301",
			"302", "303", "304", "305", "306", "307", "308", "309", "310", "311", "312", "313", "314", "315", "316", "317", "318", "319", "320", "321", "322", "323", "324", "325", "326", "327", "328", "329", "330", "331", "332", "333", "334", "335", "336", "337", "338", "339", "341", "342", "344", "346", "347", "349", "350", "351", "352", "354", "355", "356", "357", "358", "359", "360", "361", "362", "363", "364", "365", "366", "367", "368", "369", "370", "371", "372", "373", "374", "375", "376", "377", "378", "379", "380", "381", "382", "383", "384", "385", "386", "387", "388", "389", "390", "391", "392", "393", "394", "395", "396", "397", "398", "399", "400", "401", "402", "403", "404", "405", "406", "407", "408", "409", "410", "411", "412", "413", "414", "415", "416", "417", "418", "420", "421", "422", "423", "424", "425", "426", "427", "430", "431", "432", "433", "434", "435", "436", "437", "438", "439", "440", "441", "442", "443", "444", "445", "446", "447", "448", "449", "450", "451", "452", "453",
			"454", "455", "456", "457", "458", "459", "460", "461", "462", "463", "464", "465", "466", "467", "468", "469", "470", "471", "472", "473", "474", "475", "476", "477", "478", "479", "480", "481", "482", "483", "484", "485", "486", "487", "488", "489", "490", "491", "492", "493", "494", "495", "496", "497", "498", "499", "500", "501", "502", "503", "504", "505", "506", "507", "508", "509", "514", "520", "521", "522", "523", "524", "525", "526", "527", "528", "530", "531", "532", "534", "535", "537", "538", "539", "540", "541", "542", "543", "544", "545", "546", "547", "548", "549", "550", "551", "553", "554", "555", "556", "557", "558", "559", "560", "561", "562", "563", "564", "565", "566", "567", "580", "581", "582", "583", "584", "600", "601", "602", "603", "604", "605", "606", "607", "608", "609", "610", "611", "612", "613", "614", "615", "616", "617", "618", "619", "620", "622", "623", "624", "625", "626", "627", "628", "629", "630", "631", "633", "634", "635", "636", "637", "638", "639",
			"648", "650", "651", "652", "653", "654", "655", "656", "657", "658", "700", "701", "703", "704", "705", "706", "707", "708", "710", "711", "712", "713", "714", "716", "717", "718", "719", "720", "721", "722", "723", "724", "725", "728", "755", "756", "759");
	/**
	 * LAX邮编前三位
	 */
	private final static List<String> LAX = Arrays.asList("510", "511", "512", "513", "515", "516", "570", "571", "572", "573", "574", "575", "576", "577", "585", "586", "587", "588", "590", "591", "592", "593", "594", "595", "596", "597", "598", "599", "640", "641", "644", "645", "646", "647", "649", "660", "661", "662", "664", "665", "666", "667", "668", "669", "670", "671", "672", "673", "674", "675", "676", "677", "678", "679", "680", "681", "683", "684", "685", "686", "687", "688", "689", "690", "691", "692", "693", "726", "727", "729", "730", "731", "733", "734", "735", "736", "737", "738", "739", "740", "741", "743", "744", "745", "746", "747", "748", "749", "750", "751", "752", "753", "754", "757", "758", "760", "761", "762", "763", "764", "765", "766", "767", "768", "769", "770", "772", "773", "774", "775", "776", "777", "778", "779", "780", "781", "782", "783", "784", "785", "786", "787", "788", "789", "790", "791", "792", "793", "794", "795", "796", "797", "798", "799", "800", "801", "802", "803",
			"804", "805", "806", "807", "808", "809", "810", "811", "812", "813", "814", "815", "816", "820", "821", "822", "823", "824", "825", "826", "827", "828", "829", "830", "831", "832", "833", "834", "835", "836", "837", "838", "840", "841", "842", "843", "844", "845", "846", "847", "850", "851", "852", "853", "855", "856", "857", "859", "860", "863", "864", "865", "870", "871", "873", "874", "875", "876", "877", "878", "879", "880", "881", "882", "883", "884", "885", "889", "890", "891", "893", "894", "895", "897", "898", "900", "901", "902", "903", "904", "905", "906", "907", "908", "910", "911", "912", "913", "914", "915", "916", "917", "918", "919", "920", "921", "922", "923", "924", "925", "926", "927", "928", "930", "931", "932", "933", "934", "935", "936", "937", "938", "939", "940", "941", "942", "943", "944", "945", "946", "947", "948", "949", "950", "951", "952", "953", "954", "955", "956", "957", "958", "959", "960", "961", "970", "971", "972", "973", "974", "975", "976", "977", "978",
			"979", "980", "981", "982", "983", "984", "985", "986", "988", "989", "990", "991", "992", "993", "994", "995", "996", "997", "998", "999", "006", "007", "008", "009", "967", "968", "969");
	private final static String USLAX = "USLAX1";
	private final static String LAX_CODE = "LAX";
	private final static String EWR_CODE = "EWR";
	private final static String USEWR = "USEWR1";
	private final static String PACKAGEID_PREFIX = "JFTX";
	private final static String SPACE = " ";
	private final static String COMMA = ",";
	private final static String LABEL = "https://api.dhlecs.com/shipping/v4/label?format=PNG";
	private final static String ACCESS_TOKEN = "https://api.dhlecs.com/auth/v4/accesstoken";
	private final static Map<String, String> ALL_AMERICA_STATE = ApiUtil.getAllAmericaState();
	private final static Map<String, String> ALL_AMERICA_STATE2 = ApiUtil.getAllAmericaState2();
	private final Logger LOGGER = LoggerFactory.getLogger(DhlEcClient.class);

	@Override
	public ApiOrderResponse preProcessCreateOrder(ExpressOrderApi expressOrderApi) {
		String countryCode = expressOrderApi.getCountryCode();
		String consigneeState = expressOrderApi.getConsigneeState();
		if (StringUtils.isBlank(countryCode) || StringUtils.isNotBlank(countryCode) && !StringUtils.equalsIgnoreCase(countryCode, DEFAULT_CONSIGNEE_COUNTRY_CODE)) {
			return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.only.support.us"), false, null);
		}
		expressOrderApi.setCountryCode(expressOrderApi.getCountryCode().toUpperCase());
		if (StringUtils.isBlank(consigneeState)) {
			return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.consignee.state.required"), false, null);
		}
		String consigneeCompanyName = StringUtils.isBlank(expressOrderApi.getConsigneeCompanyName()) ? expressOrderApi.getConsigneeName() : expressOrderApi.getConsigneeCompanyName();
		expressOrderApi.setConsigneeCompanyName(consigneeCompanyName);
		if (StringUtils.isNotBlank(consigneeState)) {
			consigneeState = consigneeState.toUpperCase();
		}
		if (consigneeState.length() <= 2) {
			// 如果大写的能获取到就是二字代码是正确的
			String upperCaseState = ALL_AMERICA_STATE.get(consigneeState.toString());
			if (StringUtils.isBlank(upperCaseState)) {
				return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.consignee.state.error"), false, null);
			}
			expressOrderApi.setConsigneeState(consigneeState);
		} else {
			// 如果不是二字代码
			String upperCaseState = ALL_AMERICA_STATE2.get(consigneeState.toString());
			if (StringUtils.isBlank(upperCaseState)) {
				return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.consignee.state.error"), true, null);
			}
			expressOrderApi.setConsigneeState(upperCaseState);
		}
		if (StringUtils.isBlank(consigneeState)) {
			return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.consignee.state.required"), true, null);
		}
		String customerInvoiceCode = expressOrderApi.getCustomerInvoiceCode();
		if (customerInvoiceCode.length() > 30) {
			return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.customer.invoice.code.gt.30.char"), false, null);
		}
		if (customerInvoiceCode.indexOf("-") > -1 || customerInvoiceCode.indexOf("_") > -1 || customerInvoiceCode.indexOf(".") > -1) {
			return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.customer.invoice.code.not.allow.special.chars"), false, null);
		}
		String postCode = expressOrderApi.getConsigneePostCode();
		if (StringUtils.isBlank(postCode)) {
			return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.consignee.postcode.required"), false, null);
		}
		if (RegexUtil.isUsPostCode(postCode)) {
			return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.consignee.postcode.illegal"), false, null);
		}
		String postcodePrefix = postCode.substring(0, 3);
		if (LAX.indexOf(postcodePrefix) == -1 && EWR.indexOf(postcodePrefix) == -1) {
			return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.unsupport.consignee.postcode"), false, null);
		}
		if (LAX.indexOf(postcodePrefix) > -1) {
			expressOrderApi.setThirdPartyProductCode(USLAX);
		} else if (EWR.indexOf(postcodePrefix) > -1) {
			expressOrderApi.setThirdPartyProductCode(USEWR);
		}
		String address = expressOrderApi.getConsigneeAddress();
		if (address.length() > 50) {
			return new ApiOrderResponse(false, null, PropertiesUtil.getProperty("dhl.ec.consignee.address.less.then.50.chars"), false, null);
		}
		return null;
	}

	@Override
	public String mapping(ExpressOrderApi expressOrderApi) {
		Map<String, String> apiCredentials = expressOrderApi.getApiCredentials();
		Shipment shipment = new Shipment();
		shipment.setPickup(apiCredentials.get(ApiCredential.DHLECV4_PICKUP.getKey()));
		shipment.setDistributionCenter(apiCredentials.get(ApiCredential.DHLECV4_DISTRIBUTION_CENTER.getKey()));
		shipment.setOrderedProductId(apiCredentials.get(ApiCredential.DHLECV4_ORDERED_PRODUCTID.getKey()));
		// 收货人
		ConsigneeAddress consigneeAddress = new ConsigneeAddress();
		consigneeAddress.setAddress1(expressOrderApi.getConsigneeAddress());
		consigneeAddress.setCity(expressOrderApi.getConsigneeCity());
		consigneeAddress.setState(expressOrderApi.getConsigneeState());
		consigneeAddress.setCompanyName(expressOrderApi.getConsigneeCompanyName());
		consigneeAddress.setCountry(expressOrderApi.getCountryCode());
		consigneeAddress.setEmail(expressOrderApi.getConsigneeEmail());
		consigneeAddress.setName(expressOrderApi.getConsigneeName());
		consigneeAddress.setPhone(StringUtils.isBlank(expressOrderApi.getConsigneeMobile()) ? expressOrderApi.getConsigneeTelephone() : expressOrderApi.getConsigneeMobile());
		consigneeAddress.setPostalCode(expressOrderApi.getConsigneePostCode());
		shipment.setConsigneeAddress(consigneeAddress);

		// 退件地址
		ReturnAddress returnAddress = new ReturnAddress();
		returnAddress.setAddress1(apiCredentials.get(ApiCredential.DHLECV4_RETURN_ADDRESS.getKey()));
		returnAddress.setCity(apiCredentials.get(ApiCredential.DHLECV4_RETURN_CITY.getKey()));
		returnAddress.setState(apiCredentials.get(ApiCredential.DHLECV4_RETURN_STATE.getKey()));
		returnAddress.setCountry(apiCredentials.get(ApiCredential.DHLECV4_RETURN_COUNTRY.getKey()));
		returnAddress.setPostalCode(apiCredentials.get(ApiCredential.DHLECV4_RETURN_POSTALCODE.getKey()));
		returnAddress.setCompanyName(apiCredentials.get(ApiCredential.DHLECV4_RETURN_COMPANYNAME.getKey()));
		shipment.setReturnAddress(returnAddress);
		PackageDetail packageDetail = new PackageDetail();
		String code = "";
		String distributionCenter = expressOrderApi.getThirdPartyProductCode();
		if (USLAX.equalsIgnoreCase(distributionCenter)) {
			code = LAX_CODE;
		} else if (USEWR.equalsIgnoreCase(distributionCenter)) {
			code = EWR_CODE;
		}
		packageDetail.setPackageId(PACKAGEID_PREFIX + expressOrderApi.getCustomerInvoiceCode());
		packageDetail.setPackageReference(expressOrderApi.getProductCode() + SPACE + code);
		packageDetail.setContentCategory(apiCredentials.get(ApiCredential.DHLECV4_CONTENT_CATEGORY.getKey()));
		String billingReference1 = apiCredentials.get(ApiCredential.DHLECV4_CONTENT_CATEGORY.getKey());
		if (StringUtils.isBlank(billingReference1)) {
			billingReference1 = apiCredentials.get(ApiCredential.DHLECV4_CONTENT_CATEGORY.getValue());
		}
		packageDetail.setBillingReference1(billingReference1);
		List<OrderInvoice> orderInvoices = expressOrderApi.getOrderInvoices();
		Double weightDouble = 0D;
		Double totalDeclaredValue = 0D;
		List<CustomsDetail> customsDetails = new ArrayList<CustomsDetail>();
		StringBuffer descBuffer = new StringBuffer();
		for (OrderInvoice orderInvoice : orderInvoices) {
			CustomsDetail customsDetail = new CustomsDetail();
			// customsDetail.setHsCode(orderInvoice.getHsCode());
			customsDetail.setSkuNumber(orderInvoice.getSkuCode());
			customsDetail.setItemDescription(orderInvoice.getSkuNameEn() + COMMA + orderInvoice.getSkuNameCn());
			customsDetail.setPackagedQuantity(orderInvoice.getInvoicePcs());
			customsDetail.setItemValue(orderInvoice.getInvoiceAmount());
			customsDetail.setCurrency(DEFAULT_CURRENCY);
			customsDetails.add(customsDetail);
			weightDouble += (orderInvoice.getInvoiceWeight() == null ? 0D : orderInvoice.getInvoiceWeight());
			totalDeclaredValue += (orderInvoice.getInvoiceAmount() == null ? 0D : orderInvoice.getInvoiceAmount());
			if (StringUtils.isNotBlank(orderInvoice.getSkuNameEn())) {
				descBuffer.append(orderInvoice.getSkuNameEn()).append(COMMA);
			}
		}
		packageDetail.setPackageDescription(descBuffer.toString());
		// 产品信息
		ShippingCost shippingCost = new ShippingCost();
		shippingCost.setCurrency(DEFAULT_CURRENCY);
		shippingCost.setDeclaredValue(totalDeclaredValue.intValue());
		if (null == expressOrderApi.getWeight()) {
			expressOrderApi.setWeight(weightDouble);
		}
		Weight weight = new Weight();
		Double weightValue = expressOrderApi.getWeight();
		weight.setValue(ApiUtil.round(weightValue, 2));
		packageDetail.setWeight(weight);
		shipment.setPackageDetail(packageDetail);
		return new Gson().toJson(shipment);
	}

	@Override
	public ApiOrderResponse createOrder(ExpressOrderApi expressOrderApi) {
		try {
			// 前置数据校验
			ApiOrderResponse apiOrderResponse = preProcessCreateOrder(expressOrderApi);
			if (null != apiOrderResponse && apiOrderResponse.getSuccess()) {
				return apiOrderResponse;
			}
			String orderJson = mapping(expressOrderApi);
			LOGGER.info("dhlglobalmail.shipment.request.json:{}", orderJson);
			String accessToken = "";
			if (StringUtils.isBlank(accessToken)) {
				return new ApiOrderResponse(false, null, ApiOrderError.SERVICE_ACCESS_TOKEN_BLANK.getMessage(), false, null);
			}
			Map<String, String> headerMap = new HashMap<String, String>();
			headerMap.put("Authorization", "Bearer " + accessToken);
			headerMap.put("Content-type", "application/json;charset=UTF-8");
			HttpClientResult httpClientResult = HttpClientPoolUtil.doPost(LABEL, headerMap, orderJson, true);
			if (null == httpClientResult || (null != httpClientResult && StringUtils.isBlank(httpClientResult.getContent()))) {
				return new ApiOrderResponse(false, null, ApiOrderError.SERVICE_NO_DATA_RETURN.getMessage(), true, null);
			}
			String responseJson = httpClientResult.getContent();
			LOGGER.info("dhl.ec.shipment.response.result:{}", responseJson);
			if (StringUtils.isBlank(responseJson)) {
				return new ApiOrderResponse(false, null, ApiOrderError.SERVICE_NO_DATA_RETURN.getMessage(), true, null);
			}
			return Response(responseJson);
		} catch (Exception e) {
			LOGGER.error("dhl.ec.shipment.exception", e);
			return new ApiOrderResponse(false, null, ApiOrderError.CREATE_ORDER_EXCEPTION.getMessage(), false, e.getMessage());
		}
	}

	/**
	 * <获取Token.>
	 * 
	 * @param apiCredentials
	 * @return
	 * @author Henry(fba02)
	 * @version [版本号, 2020-11-17]
	 * @see [类、类#方法、类#成员]
	 */
	public String accessToken(Map<String, String> apiCredentials) {
		try {
			Map<String, String> headers = new HashMap<String, String>();
			headers.put("Content-Type", "application/x-www-form-urlencoded");
			Map<String, String> params = new HashMap<String, String>();
			params.put("grant_type", "client_credentials");
			params.put("client_id", apiCredentials.get(ApiCredential.DHLECV4_CLIENT_ID.getKey()));
			params.put("client_secret", apiCredentials.get(ApiCredential.DHLECV4_CLIENT_SECRET.getKey()));
			HttpClientResult httpClientResult = HttpClientPoolUtil.doPost(ACCESS_TOKEN, headers, params, true);
			if (null == httpClientResult) {
				return null;
			}
			String responseJson = httpClientResult.getContent();
			if (StringUtils.isBlank(responseJson)) {
				return null;
			}
			Map<String, Object> responseMap = new Gson().fromJson(responseJson, new TypeToken<Map<String, Object>>() {
			}.getType());
			if (MapUtils.isEmpty(responseMap)) {
				return null;
			}
			String accessToken = (String) responseMap.get("access_token");
			if (StringUtils.isNotBlank(accessToken)) {
				return accessToken;
			}
			return null;
		} catch (Exception e) {
			LOGGER.error("dhl.ec.access.token.error", e);
			return null;
		}
	}

	private ApiOrderResponse Response(String responseJson) {
		Map<String, Object> responseMap = new Gson().fromJson(responseJson, new TypeToken<Map<String, Object>>() {
		}.getType());
		if (responseMap.containsKey("invalidParams")) {
			StringBuffer stringBuffer = new StringBuffer();
			@SuppressWarnings("unchecked")
			List<Map<String, String>> invalidParams = (List<Map<String, String>>) responseMap.get("invalidParams");
			for (Map<String, String> map : invalidParams) {
				stringBuffer.append(map.get("reason")).append(";");
			}
			return new ApiOrderResponse(false, null, stringBuffer.toString(), responseJson);
		}
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> labels = (List<Map<String, Object>>) responseMap.get("labels");
		if (CollectionUtils.isEmpty(labels)) {
			return new ApiOrderResponse(false, null, null, responseJson);// return.result.no.label.details.node
		}
		Map<String, Object> labelMap = labels.get(0);
		ApiOrderResponse apiOrderResponse = new ApiOrderResponse();
		apiOrderResponse.setSuccess(true);
		String trackingNumber = (String) labelMap.get("trackingId");
		String labelData = (String) labelMap.get("labelData");
		String dhlPackageId = (String) labelMap.get("dhlPackageId");
		String pdfName = trackingNumber + LabelSuffix.PNG.getSuffix();
		// 当前月份
		String centrePath = ApiUtil.getCurrentDay() + File.separator;
		String relativePath = DEFAULT_LABEL_FOLDER + File.separator + centrePath;
		String folderPath = DEFAULT_LABEL_ROOT + relativePath;
		if (!new File(folderPath).exists()) {
			new File(folderPath).mkdirs();
		}
		// String fileUrl = folderPath + pdfName;
		String pngUrl = folderPath + trackingNumber + ".png";
		try {
			ApiUtil.decoderBase64File(labelData, pngUrl);
		} catch (Exception e) {
			LOGGER.error("dhl.ec.decoder.label.error", e);
		}
		String url = (String) labelMap.get("link");
		apiOrderResponse.setLabelUrl(url);
		apiOrderResponse.setTrackNumber(trackingNumber);
		apiOrderResponse.setThirdPartyorderNo(dhlPackageId);
		// 追踪号截取掉前面8位
		if (trackingNumber.length() == 30) {
			trackingNumber = trackingNumber.substring(8, trackingNumber.length());
		}
		apiOrderResponse.setTrackNumber(trackingNumber);
		apiOrderResponse.setLmLabelUrl(relativePath + pdfName);
		return apiOrderResponse;
	}

	@Override
	public ApiLabelResponse label(String trackNumber, Map<String, String> configure) {
		return null;
	}

	@Override
	public ApiLabelResponse label(String referenceNo, String trackNumber, Map<String, String> configure) {
		return null;
	}

	@Override
	public ApiLabelResponse labelRender(String trackNumber, ExpressOrderApi expressOrderApi) {
		return null;
	}

	@Override
	public void tracking(String trackNumber, Map<String, String> configure) {
	}

	@Override
	public String trackNumber(String code, Map<String, String> configure) {
		return null;
	}
}